package main

import "fmt"

func main() {
	//test1()
	//test2()
	res := partition("aab")
	fmt.Println(res)
}

//var a [][]int

// go中如果要引用传递，一定要使用指针
func test1() {
	allResults := [][]int{}
	oneResult := []int{}
	oneResult = append(oneResult, 3, 4)
	allResults = append(allResults, oneResult)
	//这里oneResult是复制了一个副本放入allResults中的
	fmt.Println(allResults) //[[3 4]]

	//这里修改oneResult不会影响allResults已有的oneResult，因为那个是副本
	oneResult = append(oneResult, 6, 7)
	allResults = append(allResults, oneResult)
	fmt.Println(allResults) //[[3 4] [3 4 6 7]]

	//a = append(a, []int{2, 3})
	//fmt.Println(a)

}

func test2() {
	a := "yanzhibo"
	fmt.Println(a[3])      //122
	fmt.Printf("%c", a[3]) //z
}

func partition(s string) [][]string {
	allResults := [][]string{}
	oneResult := []string{}
	helper(s, 0, oneResult, allResults)
	return allResults
}

func helper(s string, index int, oneResult []string, allResults [][]string) {
	if index > len(s)-1 {
		allResults = append(allResults, append([]string{}, oneResult...))
		return
	}
	for i := index; i <= len(s)-1; i++ {
		subStr := s[index : i+1]
		if isPalindrom(subStr) {
			oneResult = append(oneResult, subStr)
			helper(s, i+1, oneResult, allResults)
			oneResult = oneResult[:len(oneResult)-1]
		}
	}
}

func isPalindrom(str string) bool {
	l := 0
	r := len(str) - 1
	for l < r {
		if str[l] == str[r] {
			l++
			r--
		} else {
			return false
		}
	}
	return true
}
